package com.cloud.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cloud.order.Service.OrderService;

import model.bean.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 韩先楚
 */
@RestController
public class OrderController {


    @Autowired
    private OrderService orderService;

    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId){
        return orderService.createOrder(productId,userId);

    }


    @GetMapping("/orderkill")
    @SentinelResource(value = "orderkill-test",fallback = "createKIllOrderFallBack")
    public Order createKIllOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId){
        return orderService.createOrder(productId,userId);
    }

    public Order createKIllOrderFallBack(Long userId, Long productId, Throwable e){
        System.out.println("orderkill- 被限流");
        Order order = new Order();
        order.setId(123456789L);
        order.setAddress("秒杀被限流了");
        return order;
    }


    @GetMapping("/readDB")
    public String readDB(){
      orderService.lll();
        return "readDB";
    }

    @GetMapping("/writeDB")
    public String writeDB(){
        return "writeDB";
    }


}
